package com.android.ddfafu.ui.home;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.android.ddfafu.MapApplication;
import com.android.ddfafu.R;
import com.android.ddfafu.result.CameraResult;
import com.android.ddfafu.result.MyBundleData;
import com.android.ddfafu.utils.UploadImageThead;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import java.util.List;

public class HomeFragment extends Fragment{
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LatLng latLng;
    private MapApplication mapApplication;

    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {


        Log.i("----Fragment----","onCreateView");
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        mMapView = root.findViewById(R.id.bmapView);
        initMap();
        homeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        return root;
    }

    private void initMap() {
        mMapView.showZoomControls(false);
        //获取地图控件引用
        mBaiduMap = mMapView.getMap();
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMyLocationEnabled(true);

        mapApplication = (MapApplication) getActivity().getApplication();

        //通过异步执行,防止线程阻塞 因为Application会等待Activity的onCreate执行完后才开始定位
        new Thread(() -> {
            while (mapApplication.getLatLng() == null) {
                try {
                    Thread.sleep(1000);
                    System.out.println("正在定位");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("定位成功！！");
            latLng = mapApplication.getLatLng();
            BDLocation bdLocation = mapApplication.getBdLocation();
            System.out.println("获取BDlcation成功" + latLng.toString());
            // 构造定位数据
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(bdLocation.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(bdLocation.getLatitude())
                    .longitude(bdLocation.getLongitude()).build();
            // 设置定位数据
            mBaiduMap.setMyLocationData(locData);

            //设置地图缩放至定位点
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(latLng).zoom(21.0f);
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        }).start();

        mBaiduMap.clear();
        //通过application 获取照片信息 并显示在地图上
        List<Bundle> bundleList = mapApplication.getBundleList();
        if (bundleList.size() > 0) {
            for (Bundle bundle : bundleList) {
                MyBundleData myBundleData = (MyBundleData) bundle.getSerializable("MARKER");
                OverlayOptions options = myBundleData.getOptions();
                mBaiduMap.addOverlay(options);
            }
        }

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();


    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }


}